package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingcartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Base64Utils;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingcartMapper shoppingcartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    public void insert(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingcart=new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingcart);
        Long userid=BaseContext.getCurrentId();
        shoppingcart.setId(userid);

        List<ShoppingCart> list=shoppingcartMapper.list(shoppingcart);

        if (list!=null&&list.size()>0){
            ShoppingCart cart=list.get(0);
            cart.setNumber(cart.getNumber()+1);
            shoppingcartMapper.updatecount(cart);
        }else {
            Long dishid=shoppingCartDTO.getDishId();
            Long setmealid=shoppingCartDTO.getSetmealId();
            if (dishid!=null){

                Dish dish=dishMapper.getbyid(dishid);
                shoppingcart.setName(dish.getName());
                shoppingcart.setImage(dish.getImage());
                shoppingcart.setAmount(dish.getPrice());

            }else {

                Setmeal setmeal =setmealMapper.getById(setmealid);
                shoppingcart.setName(setmeal.getName());
                shoppingcart.setImage(setmeal.getImage());
                shoppingcart.setAmount(setmeal.getPrice());

            }

            shoppingcart.setNumber(1);
            shoppingcart.setCreateTime(LocalDateTime.now());
            shoppingcartMapper.insert(shoppingcart);
        }

    }

    public List<ShoppingCart> list() {
        Long userid=BaseContext.getCurrentId();
        ShoppingCart shoppingCart=ShoppingCart.builder()
                .userId(userid)
                .build();
        List<ShoppingCart> list=shoppingcartMapper.list(shoppingCart);
        return list;
    }

    public void deleteAll() {
        Long userid=BaseContext.getCurrentId();
        shoppingcartMapper.deleteAll(userid);

    }

    public void delete(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart=new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);

        shoppingCart.setUserId(BaseContext.getCurrentId());

        List<ShoppingCart> list=shoppingcartMapper.list(shoppingCart);
        if (list!=null&&list.size()>0){
            shoppingCart=list.get(0);

            Integer number=shoppingCart.getNumber();
            if (number==1){
                shoppingcartMapper.deletebyid(shoppingCart.getId());
            }else {
                shoppingCart.setNumber(number-1);
                shoppingcartMapper.updatecount(shoppingCart);
            }
        }

    }
}
